package com.kaliterre.kalidroid.broadcastreceiver;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.kaliterre.kalidroid.dal.BatteryLogDAL;
import com.kaliterre.kalidroid.entity.BatteryLog;

/**
 * @author FIL A2 - Alyssa Boucher, Maxime Bureau, Yohan Mercier, Nicolas Mortier, Zakaria Oualkadi.
 *
 *	Custom BroadcastReceiver to catch power connection events.
 *	
 */
public class PowerConnectionReceiver extends BroadcastReceiver {

	/* (non-Javadoc)
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent) {		
		
		Log.e("", "************************ "+ intent.getAction() + " ************************");
		int status = 0;
		int level = 0;
		int scale = 0;
		int temp = 0;
		int voltage = 0;
		
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
	
		if(intent.getAction() == Intent.ACTION_POWER_CONNECTED || intent.getAction() == Intent.ACTION_POWER_DISCONNECTED)
		{
			Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
			
			status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
			level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
			scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
			temp = batteryIntent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
			voltage = batteryIntent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
        	String batteryLevel = String.valueOf(level/scale*100);
		}
		else
		{
			status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
			level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
			scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
			temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
			voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
        	String batteryLevel = String.valueOf(level/scale*100);
		}  
        
        // Create the log.
        BatteryLog log = new BatteryLog(formatDate.format(new Date()), intent.getAction(), String.valueOf(level),String.valueOf(voltage), String.valueOf(temp), String.valueOf(status));
        // Create object to access to the DB.
        BatteryLogDAL battryLogDal = new BatteryLogDAL(context);
        
        // Open the access to the DB.
        battryLogDal.open();
        // Insert the battery log.
        battryLogDal.insertBatteryLog(log);

        // Close the access to the DB.
        battryLogDal.close();
		
	}
	
	public IntentFilter getIntentFilterActions()
	{
		IntentFilter intentFilterBattery = new IntentFilter();
		intentFilterBattery.addAction(Intent.ACTION_BATTERY_CHANGED);
		intentFilterBattery.addAction(Intent.ACTION_POWER_CONNECTED);
		intentFilterBattery.addAction(Intent.ACTION_POWER_DISCONNECTED);
		
		return intentFilterBattery;
	}


}
